// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import BaseUI from "../framwork/BaseUI";
import GameData from "./data/GameData";
import MonsterData from "./data/MonsterData";
import PlayerData from "./data/PlayerData";
import playerData from "./data/PlayerData";
import Msg from "./Msg";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Battle extends BaseUI {
    private static _instance: Battle = null;
    public static getInstance(): Battle {
        return Battle._instance;
    }
    baseUI_onLoad(): void {
        Battle._instance = this;
        this.init();
        console.log(playerData);
    }
    init_data(d: any): void {

    }
    init(){
        this.resetView();
        this.monsterTimer = 0;
        this.playerTimer = 0;
        this.SetLabel("cengNum",GameData.cengshu.toString());
    }
    
    
    resetView(){
        // this.SetLabel("playerHPNum",this.playerInfo.Hp.toString() + "/" + this.playerInfo.MaxHp)
        // this.SetProgressBar("playerProgress",this.playerInfo.Hp/this.playerInfo.MaxHp)
        // this.SetLabel("monsterHPNum",this.monsterInfo.Hp.toString() + "/" + this.monsterInfo.MaxHp)
        // this.SetProgressBar("monsterProgress",this.monsterInfo.Hp/this.monsterInfo.MaxHp)
        
    }
    baseUI_onDestroy(): void {

    }
    onBtn_click(target: cc.Button, onBtn_name: string): number | void {
        switch (onBtn_name) {
            case "btn_juese":
                break;
            case "btn_lastCeng":
                GameData.cengshu--;
                this.init();
                break;
            case "btn_nextCeng":
                GameData.cengshu++;
                this.init();
                break;
            default:
                break;
        }
    }

    update(dt: number): void {
        this.playerAtt(dt);
        this.monsterAtt(dt);
    }
    playerTimer: number = 0;
    playerAtt(dt: number){
        this.playerTimer += dt;
        if(this.playerTimer >= PlayerData.SD){
            this.playerTimer = 0;
            let playerNode:cc.Node = this.GetGameObject("player");
            cc.tween(playerNode).by(0.1,{x:10}).by(0.1,{x:-10})
            .call(()=>{
                
            })
            .start();
            console.log("玩家攻击")
        }
    }
    monsterTimer: number = 0;
    monsterAtt(dt: number){
        this.monsterTimer += dt;
        if(this.monsterTimer >= MonsterData.GS){
            this.monsterTimer = 0;
            let monsterNode:cc.Node = this.GetGameObject("monster");
            cc.tween(monsterNode).by(0.1,{x:-10}).by(0.1,{x:10})
            .call(()=>{
                PlayerData.HP -= MonsterData.ATK;
                this.resetView();
                if(this.playerInfo.Hp <= 0){
                    this.createUI(Msg.prefabUrl,Battle.getInstance().getView(),{msg:"你嘎了！！！\n 自动为您降一层"})
                    this.cengshu--
                    this.init();
                }
            })
            .start();
            console.log("怪物攻击")
        }
    }
    getView(){
        return this.GetGameObject("view");
    }
}
